<?php
require 'includes/config.php';
require 'includes/sanitize.php';
require 'includes/makeRequest.php';
$data = sanitize($_POST);
// Check if user submitted enough data
if (isset($data['submit']) && isset($data['username']) && isset($data['password'])) {
	// Request to backend to get some data for the session
	$request = new makeRequest($backend.'login.php?method=login&username='.$data['username'].
	'&password='.$data['password'], 'GET');
	$request = $request->makeRequest();
	$rbody = $request[0];
	$http_code = $request [1];
	$userobj = json_decode($rbody);
	// Check for successful request
	if($http_code == 200){
		session_start();
		// Set the session data
		$_SESSION['uid'] = $userobj[0]->{'id'};
		$_SESSION['logged_in'] = true;
		$_SESSION['username'] = $userobj[0]->{'username'};
		$_SESSION['accesstoken'] = $userobj[0]->{'accesstoken'};
		header ('location:index.php');
	}else{ // If the user fails to login
		$msg = "Something went wrong, ".$http_code;
	}
}

require 'includes/header.php';
require 'includes/topmenu.php';
echo '<div id="kaka">';
require 'includes/sidemenu.php'
?>
<div id="content">
	<div id=info>
		<br />
		<form name="login" method="post" action="<?php echo $PHP_SELF; ?>">
			Username: <input type="text" name="username" value="Username" /><br />
			Password: <input type="password" name="password" value="Password" /><br />
			<input type="submit" value="submit" name="submit" />
		</form>
		<br /> <a href=signup.php>Signup</a>
	</div>
	
	
	
	
<?php
require 'includes/footer.php';
?>